<?php
namespace app\kccsCourse\controller;
use app\common\Base;
use think\Db;
use app\common\Excel;

class CourseSignupRecord extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		if(request()->isPost()){
			$page = input('page');
			$rows = input('rows');
			$where["xb.IS_VALID"] = 1;
			$termId = input("termId");
			$grade = input("grade");
			$clazz = input("clazz");
			$courseId = input("courseId");
			$studentName = input("studentName");
			$where["xb.TERM_ID"] = $termId ? $termId : currTerm()["termId"];
			if($grade) {
				$array = explode("_", $grade);
				$where["xs.年级号"] = $array[0];
				$where["xs.学籍类型"] = $array[1];
			}
			if($clazz) $where["xs.班号"] = $clazz;
			if($courseId) $where["kc.ID"] = $courseId;
			if($studentName) $where["xs.姓名"] = $studentName;
			$result = Db::table("t_kccs_course_info kc")
					->join("t_kccs_student_sign xb","kc.ID = xb.COURSE_ID","INNER")
					->join("学期名称 xq","xq.ID = xb.TERM_ID","INNER")
					->join("学生信息 xs","xs.ID = xb.STUDENT_ID","INNER")
					->field("xq.ID termId,xq.学期名称 termName,kc.ID courseId,kc.COURSE_NAME courseName,kc.COURSE_TYPE courseType,kc.COURSE_NO courseNo,kc.CLASS_TEACHER classTeacher,xb.SIGN_CLASS_DATE signClassDate,xs.ID studentId,xs.姓名 studentName,xs.学号 studentNum,xs.学籍类型 studyType,xs.年级号 grade,xs.班号 clazz")
					->where($where)
					->page($page, $rows)
					->order("courseType,courseNo,grade desc,clazz,studentNum")
					->select();
			$count = Db::table("t_kccs_course_info kc")
					->join("t_kccs_student_sign xb","kc.ID = xb.COURSE_ID","INNER")
					->join("学期名称 xq","xq.ID = xb.TERM_ID","INNER")
					->join("学生信息 xs","xs.ID = xb.STUDENT_ID","INNER")
					->where($where)
					->count();
			foreach ($result as $key => $obj) {
				$result[$key]["gradeClass"] = getCommonGradeName($obj["grade"],$obj["studyType"])."(".$obj["clazz"].")班";
			}
			$grid['rows'] = $result;
			$grid['total'] = $count;
			return $grid;
		}
	}

	public function courseList() {
		$result = Db::table("T_KCCS_COURSE_INFO")
				  ->field("ID value, COURSE_NAME text")
				  ->where("IS_AGREE = 1")
				  ->select();
		$array = array(["text"=>"请选择","value"=>""]);
		return array_merge($array,$result);
	}

	public function exportExcel() {
			$where["xb.IS_VALID"] = 1;
			$termId = input("termId");
			$grade = input("grade");
			$clazz = input("clazz");
			$courseId = input("courseId");
			$studentName = input("studentName");
			$where["xb.TERM_ID"] = $termId ? $termId : currTerm()["termId"];
			if($grade) {
				$array = explode("_", $grade);
				$where["xs.年级号"] = $array[0];
				$where["xs.学籍类型"] = $array[1];
			}
			if($clazz) $where["xs.班号"] = $clazz;
			if($courseId) $where["kc.ID"] = $courseId;
			if($studentName) $where["xs.姓名"] = $studentName;
			$result = Db::table("t_kccs_course_info kc")
					->join("t_kccs_student_sign xb","kc.ID = xb.COURSE_ID","INNER")
					->join("学期名称 xq","xq.ID = xb.TERM_ID","INNER")
					->join("学生信息 xs","xs.ID = xb.STUDENT_ID","INNER")
					->field("xq.ID termId,xq.学期名称 termName,kc.ID courseId,kc.COURSE_NAME courseName,kc.COURSE_TYPE courseType,kc.COURSE_NO courseNo,kc.CLASS_TEACHER classTeacher,xb.SIGN_CLASS_DATE signClassDate,xs.ID studentId,xs.姓名 studentName,xs.学号 studentNum,xs.学籍类型 studyType,xs.年级号 grade,xs.班号 clazz")
					->where($where)
					->order("courseType,courseNo,grade desc,clazz,studentNum")
					->select();
		foreach ($result as $key => $obj) {
			$result[$key]["gradeClass"] = getCommonGradeName($obj["grade"],$obj["studyType"])."(".$obj["clazz"].")班";
		}
		//excel表头内容
		$header = ["课程分类"=>"courseType","课程编号"=>"courseNo","辅导老师"=>"classTeacher","上课时间"=>"signClassDate","学生班级"=>"gradeClass","学生学号"=>"studentNum","学生姓名"=>"studentName"];
		Excel::toExcel($result,'校本课程学生报名表',$header);
		exit;
	}

}
